package oliver;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class BoxUtils {

    private static final String PREFIX = "group_";

    public static Map<String, List<Box>> mixBoxes(List<Box> boxes) {
        Map<String, List<Box>> mixedResult = new HashMap<>();
        int mixedCount = 0;
        for (Box box : boxes) {
            if (box.getCarton() > 0) {
                mixedCount++;
                List<Box> singleBoxes = new ArrayList<>();
                singleBoxes.add(box);
                mixedResult.put(PREFIX + mixedCount, singleBoxes);
            }
            if (box.getCarton() == 0) {
                List<Box> mixedBoxes = mixedResult.get(PREFIX + mixedCount);
                if (mixedBoxes != null) {
                    mixedBoxes.add(box);
                }
            }
        }
        return mixedResult;
    }

    public static void main(String[] args) {
        List<Box> boxes = new ArrayList<>();
        boxes.add(new Box(1, 1, "A", true));
        boxes.add(new Box(2, 0, "B", true));
        boxes.add(new Box(3, 0, "C", true));
        boxes.add(new Box(4, 2, "D", true));
        boxes.add(new Box(5, 0, "E", true));
        boxes.add(new Box(6, 3, "F", false));
        Map<String, List<Box>> mixedResult = mixBoxes(boxes);
        System.out.println(mixedResult);
    }
}
